package com.wanglei.config;

//import com.wanglei.filter.JwtAuthenticationTokenFilter;
import com.wanglei.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
//@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Bean
    //加这行为 id 添加密码编码映射
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
//                // 对于登录接口 允许匿名访问
                .antMatchers("/user/login").anonymous()
                .antMatchers("/content/tag/list").authenticated()
                .antMatchers("/content/tag").authenticated() //添加标签
                .antMatchers("/getInfo").authenticated() //获取用户权限相关信息
                .antMatchers("/getRouters").authenticated() //获取用户权限菜单 一级 二级...
                .antMatchers("/user/logout").authenticated()
                .antMatchers("/content/tag/{id}").authenticated()
                .antMatchers("/content/category/export").authenticated()
                .antMatchers("/content/article").authenticated()
                .antMatchers("/content/article/{id}").authenticated()
                .antMatchers("/content/article/list").authenticated()
//                .antMatchers("/system/menu").authenticated()
//                .antMatchers("/system/menu/list").authenticated()
//                .antMatchers("/system/menu/{id}").authenticated()
//                .antMatchers("/system/menu/{menuId}").authenticated()
//                .antMatchers("/system/menu/treeselect").authenticated()
//                .antMatchers("/system/menu/roleMenuTreeselect/{id}").authenticated()
//                .antMatchers("/system/role/list").authenticated()
//                .antMatchers("/system/role/changeStatus").authenticated()
//                .antMatchers("/system/role").authenticated()
//                .antMatchers("/system/role/{id}").authenticated()
                // 除上面外的所有请求全部不需要鉴权认证
                .anyRequest().permitAll();

        //关闭默认的退出登陆 这样就可以使用自己写的退出登录功能了
        http.logout().disable();
        //把token校验过滤器添加到过滤器链中
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //配置异常处理
        http.exceptionHandling()
                //配置认证失败处理器
                .authenticationEntryPoint(authenticationEntryPoint)
                //配置授权失败处理器
                .accessDeniedHandler(accessDeniedHandler);


        //SpringSecurity允许跨域
        http.cors();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
